package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC575 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int distributeCandies(int[] candyType) {
            Map<Integer, Integer> typeCnt = new HashMap<>();
            for (int type : candyType) {
                typeCnt.put(type, typeCnt.getOrDefault(type, 0) + 1);
            }

            TreeMap<Integer, Integer> disMap = new TreeMap<>();
            for (Integer type : typeCnt.keySet()) {
                disMap.put(typeCnt.get(type), disMap.getOrDefault(typeCnt.get(type), 0) + 1);
            }

            int requireLen = candyType.length / 2, typeSum = 0;
            for (Integer dis : disMap.keySet()) {
                if (typeSum + disMap.get(dis) < requireLen) {
                    typeSum += disMap.get(dis);
                } else return requireLen;
            }

            return typeSum;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.distributeCandies(InputUtil.toIntegerArray("[1,1,2,2,3,3]")));
        System.out.println(solution.distributeCandies(InputUtil.toIntegerArray("[1,1,2,3]")));
        System.out.println(solution.distributeCandies(InputUtil.toIntegerArray("[6,6,6,6]")));
        System.out.println(solution.distributeCandies(InputUtil.toIntegerArray("[1,1,1,1,2,2,2,3,3,3]")));
    }

    public static void main(String[] args) throws Exception {
        LC575 an = new LC575();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
